package com.xuelang.config;


// import com.alibaba.fastjson2.support.spring6.FastJsonHttpMessageConverter; // Removed for Spring Boot 3.x compatibility
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.xuelang.component.LoggingRequestInterceptor;
import com.xuelang.pipeline.client.common.util.HttpHelper;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;

/**
 * Http工具配置类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Configuration
public class HttpConfig {

    /**
     * 发送http2.0请求
     *
     * @param restTemplate
     * @return
     */
    @Bean("http2Helper")
    public HttpHelper http2Helper(@Qualifier("restTemplateHttp2") RestTemplate restTemplate) {
        return new HttpHelper(restTemplate);
    }

    /**
     * 发送http1.1请求
     *
     * @param restTemplate
     * @return
     */
    @Bean("http1Helper")
    public HttpHelper http1Helper(@Qualifier("restTemplateHttp1") RestTemplate restTemplate) {
        return new HttpHelper(restTemplate);
    }

    //底层采用OkHttp3来发起http/2请求
    @Bean
    public OkHttpClient okHttpClient() {
        return new OkHttpClient.Builder()
//                .readTimeout(Duration.ofSeconds(10))
//                .writeTimeout(Duration.ofSeconds(10))
//                .connectTimeout(Duration.ofSeconds(10))
//                .callTimeout(Duration.ofSeconds(20))
                .protocols(Collections.singletonList(Protocol.H2_PRIOR_KNOWLEDGE))
//                .dns(new EngDNS())    //IP选择，优先使用IPv4
                .build();
    }

    /**
     * 提供发送HTTP2.0请求协议
     *
     * @return
     */
    @Bean("restTemplateHttp2")
    public RestTemplate restTemplateHttp2(OkHttpClient okHttpClient) {
        OkHttp3ClientHttpRequestFactory okHttp3ClientHttpRequestFactory = new OkHttp3ClientHttpRequestFactory(okHttpClient);

        RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(okHttp3ClientHttpRequestFactory));
        restTemplate.setMessageConverters(Arrays.asList(
                new MappingJackson2HttpMessageConverter(),
                new FastJsonHttpMessageConverter(),
                new StringHttpMessageConverter(StandardCharsets.UTF_8)));
        restTemplate.setInterceptors(Collections.singletonList(new LoggingRequestInterceptor()));
        return restTemplate;
    }

    /**
     * 提供发送HTTP1.1请求协议
     *
     * @return
     */
    @Bean("restTemplateHttp1")
    public RestTemplate restTemplateHttp1() {
        SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory();
//        simpleClientHttpRequestFactory.setConnectTimeout(10*1000);
//        simpleClientHttpRequestFactory.setReadTimeout(10*1000);

        RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(simpleClientHttpRequestFactory));
        restTemplate.setMessageConverters(Arrays.asList(
                new MappingJackson2HttpMessageConverter(),
                new StringHttpMessageConverter(StandardCharsets.UTF_8)));
        restTemplate.setInterceptors(Collections.singletonList(new LoggingRequestInterceptor()));
        return restTemplate;
    }


}
